package fr.pud.client.model;

import java.io.Serializable;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.utils.DOMParser;

/**
 * The representation of the Vehicle.
 * Apr 9, 2012 - OSE.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         Vehicle.java
 */
public class Vehicle implements Serializable {
    /**
     * A serial UID corresponding to the vehicle class
     */
    private static final long serialVersionUID = -6212573999510903365L;
    /**
     * The capacity = integer
     */
    private int               capacity;
    /**
     * The max drive duration = integer
     */
    private int               maxDriveDuration;
    /**
     * The starting point = PUDStation
     */
    private PUDStation        startingPoint;
    /**
     * The ending point = PUDStation
     */
    private PUDStation        endingPoint;

    /**
     * Default constructor : everything to default.
     */
    public Vehicle() {
        this.capacity = 0;
        this.maxDriveDuration = 0;
        this.startingPoint = new PUDStation();
        this.endingPoint = new PUDStation();
    }

    /**
     * @param capac
     *            the capacity
     * @param auto
     *            the max drive duration
     * @param addressStartingPoint
     *            the address of the starting point
     * @param openingStartingPoint
     *            the opening time of the starting point
     * @param closingStartingPoint
     *            the closing time of the starting point
     * @param addressEndingPoint
     *            the address of the ending point
     * @param openingEndingPoint
     *            the opening time of the ending point
     * @param closingEndingPoint
     *            the closing time of the ending point.
     */
    public Vehicle(int capac, int auto, Address addressStartingPoint,
            Time openingStartingPoint, Time closingStartingPoint,
            Address addressEndingPoint, Time openingEndingPoint,
            Time closingEndingPoint) {
        this(capac, auto, new PUDStation(addressStartingPoint,
                openingStartingPoint, closingStartingPoint), new PUDStation(
                addressEndingPoint, openingStartingPoint, closingEndingPoint));
    }

    /**
     * Constructor with parameters.
     * @param c
     *            the capacity
     * @param a
     *            the max drive duration
     * @param s
     *            the starting point
     * @param e
     *            the ending point.
     */
    public Vehicle(int c, int a, PUDStation s, PUDStation e) {
        this.capacity = c;
        this.maxDriveDuration = a;
        this.startingPoint = s;
        this.endingPoint = e;
    }

    /**
     * @return the capacity
     */
    public int getCapacity() {
        return this.capacity;
    }

    /**
     * @return the ending point.
     */
    public PUDStation getEndingPoint() {
        return this.endingPoint;
    }

    /**
     * @return the max drive duration.
     */
    public int getMaxDriveDuration() {
        return this.maxDriveDuration;
    }

    /**
     * @return the starting point.
     */
    public PUDStation getStartingPoint() {
        return this.startingPoint;
    }

    /**
     * @param c
     *            the new capacity.
     */
    public void setCapacity(int c) {
        this.capacity = c;
    }

    /**
     * @param endingPoint
     *            the new ending point.
     */
    public void setEndingPoint(PUDStation endingPoint) {
        this.endingPoint = endingPoint;
    }

    /**
     * @param a
     *            the new max drive duration
     */
    public void setMaxDriveDuration(int a) {
        this.maxDriveDuration = a;
    }

    /**
     * @param startingPoint
     *            the new starting point.
     */
    public void setStartingPoint(PUDStation startingPoint) {
        this.startingPoint = startingPoint;
    }

    public String toHTML() {
        return "<h4 style=\"color:#FFA500\">"
                + LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.VEHICLE_CAPACITY)
                + " : "
                + this.getCapacity()
                + " </h4>"
                + "<h4 style=\"color:#800080\">"
                + LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.VEHICLE_AUTONOMY)
                + " : "
                + this.getMaxDriveDuration()
                + " </h4>"
                + "<h4 style=\"color:green\">"
                + LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.VEHICLE_STARTING_POINT_ADDRESS)
                + " : </h4>"
                + this.getStartingPoint().toHTML()
                + "<h4 style=\"color:red\">"
                + LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.VEHICLE_ENDING_POINT_ADDRESS)
                + " : </h4>" + this.getEndingPoint().toHTML();
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.VEHICLE_CAPACITY)
                + " : "
                + this.getCapacity()
                + "\n"
                + LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.VEHICLE_AUTONOMY)
                + " : "
                + this.getMaxDriveDuration()
                + "\n"
                + LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.VEHICLE_STARTING_POINT_ADDRESS)
                + " : "
                + this.getStartingPoint()
                + "\n"
                + LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.VEHICLE_ENDING_POINT_ADDRESS)
                + " : " + this.getEndingPoint();
    }

    /**
     * @param d
     *            the document associated with the new element.
     * @return a new element : a xml representation of a vehicle.
     */
    public Element toXML(Document d) {
        return this.toXML(d, "vehicle");
    }

    /**
     * @param d
     *            the document associated with the new element.
     * @param name
     *            the root name
     * @return the new element.
     */
    public Element toXML(Document d, String name) {
        Element root = d.createElement(name);
        root.appendChild(this.startingPoint.toXML(d, "startingPoint"));
        root.appendChild(this.startingPoint.toXML(d, "endingPoint"));
        DOMParser.getElementAppended(d, "capacity", "" + this.getCapacity(),
                root);
        DOMParser.getElementAppended(d, "autonomy",
                "" + this.getMaxDriveDuration(), root);
        return root;
    }
}
